//
//  FilterPage_Vc.swift
//  MikerShop
//
//  Created by eme on 2016/11/30.
//  Copyright © 2016年 eme. All rights reserved.
//


/*
 owner:cy
 update:2017年03月30日15:56:07
 info: 筛选首页
 */
import RxSwift
import UtilCore
import RickonCore

class FilterPage_Vc: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    @IBOutlet weak var reset_Btn: UIButton!
    
    @IBOutlet weak var submit_Btn: UIButton!
    
    @IBOutlet weak var pp_Name_Lb: UILabel!
    @IBOutlet weak var lm_Name_Lb: UILabel!
    @IBOutlet weak var sx_Name_Lb: UILabel!
    
    @IBOutlet weak var minamount_Tf: UITextField!
    @IBOutlet weak var minamount_V: UIView!
    @IBOutlet weak var maxamount_Tf: UITextField!
    @IBOutlet weak var maxamount_V: UIView!
    @IBOutlet weak var moq_Tf: UITextField!
    @IBOutlet weak var moq_V: UIView!
    
    @IBOutlet weak var pp_Separate_Line_V: UIView!
    @IBOutlet weak var lm_Separate_Line_V: UIView!
    @IBOutlet weak var sx_Separate_Line_V: UIView!
    @IBOutlet weak var jg_Separate_Line_V: UIView!
    @IBOutlet weak var jg_H_Separate_Line_V: UIView!
    @IBOutlet weak var qpl_Separate_Line_V: UIView!
    @IBOutlet var pp_Tap: UITapGestureRecognizer!
    @IBOutlet var lm_Tap: UITapGestureRecognizer!
    @IBOutlet var sx_Tap: UITapGestureRecognizer!
    /*----------------------------   自定义属性区域    ----------------------------*/
    /*
     这块主要用于从品牌详情界面进来的，如果从品牌详情界面进来 则 品牌列表筛选只能选中品牌，没有不限
     */
    var brid:String?
    var searchProductModel:SearchProductModel?
    /// 当点击确定的时候 先见筛选的条件传到SearchProducts_Vc
    var didSelectFilterParam_Closure:((_ item:SearchProductModel) -> Void)?
    /****************************Storyboard 绑定方法区域****************************/
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        /// 更新界面显示
        if let searchProductModel = self.searchProductModel {
            updateViewByModel(searchProductModel)
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let identifier = segue.identifier { // 检查是否 nil
            switch identifier {
            case "FilterBrand_Vc":
                if let vc = segue.destination as? FilterBrand_Vc {
                    vc.brlist = self.searchProductModel?.brlist
                    /*
                     这块主要用于从品牌详情界面进来的，如果从品牌详情界面进来 则 品牌列表筛选只能选中品牌，没有不限
                     */
                    vc.brid = self.brid
                    vc.hasSelected_BrModel = self.searchProductModel?.hasSelected_BrModel
                    vc.didSelectBrandModel_Closure = { [unowned self] brandModel in
                        self.searchProductModel?.hasSelected_BrModel = brandModel
                    }
                }
            case "FilterType_Vc":
                if let vc = segue.destination as? FilterType_Vc {
                    vc.ptlist = self.searchProductModel?.ptlist
                    vc.hasSelected_ptModel = self.searchProductModel?.hasSelected_PtModel
                    vc.didSelectPtModel_Closure = { [unowned self] ptModel in
                        self.searchProductModel?.hasSelected_PtModel = ptModel
                    }
                }
            case "FilterAttr_Vc":
                if let vc = segue.destination as? FilterAttr_Vc {
                    if let searchProductModel = self.searchProductModel {
                        let palist = searchProductModel.palist.map{$0.copy() as! PalistModel}
                        vc.palist = palist
                    }
                    vc.didSelectPaModel_Closure = {[unowned self] palist in
                        if let palist = palist {
                            self.searchProductModel?.palist = palist
                        }else{
                            self.searchProductModel?.palist = []
                        }
                    }
                }
            default: break
            }
        }
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
    }
    /**
     app 主题 设置
     */
    override func setViewTheme(){
        self.minamount_V.changeBorderColor(Theme.v.separate_Line, cornerRadius: 1.0)
        self.maxamount_V.changeBorderColor(Theme.v.separate_Line, cornerRadius: 1.0)
        self.moq_V.changeBorderColor(Theme.v.separate_Line, cornerRadius: 1.0)
        self.pp_Separate_Line_V.backgroundColor = Theme.v.separate_Line
        self.lm_Separate_Line_V.backgroundColor = Theme.v.separate_Line
        self.sx_Separate_Line_V.backgroundColor = Theme.v.separate_Line
        self.jg_Separate_Line_V.backgroundColor = Theme.v.separate_Line
        self.jg_H_Separate_Line_V.backgroundColor = Theme.v.separate_Line
        self.qpl_Separate_Line_V.backgroundColor = Theme.v.separate_Line
    }
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        self.submit_Btn
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
                if let didSelectFilterParam_Closure = self.didSelectFilterParam_Closure{
                    didSelectFilterParam_Closure(self.searchProductModel!)
                }
                _ = self.navigationController?.popViewController(animated: true)
            })
            .disposed(by: disposeBag)
        self.reset_Btn
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
                self.resetSearchProductDataData()
            })
            .disposed(by: disposeBag)
        
        self.pp_Tap
            .rx.event
            .subscribe(onNext: { [unowned self] ( _ ) in
                if let searchProductModel = self.searchProductModel  {
                    self.performSegue(withIdentifier: "FilterBrand_Vc", sender: searchProductModel)
                }
            })
            .disposed(by: disposeBag)
        self.lm_Tap
            .rx.event
            .subscribe(onNext: { [unowned self] ( _ ) in
                if let searchProductModel = self.searchProductModel  {
                    self.performSegue(withIdentifier: "FilterType_Vc", sender: searchProductModel)
                }
            })
            .disposed(by: disposeBag)
        self.sx_Tap
            .rx.event
            .subscribe(onNext: { [unowned self] ( _ ) in
                if let searchProductModel = self.searchProductModel  {
                    self.performSegue(withIdentifier: "FilterAttr_Vc", sender: searchProductModel)
                }
            })
            .disposed(by: disposeBag)
        
    }
}
extension FilterPage_Vc{
    /// 重置数据
    func resetSearchProductDataData() -> Void {
        if let searchProductModel = self.searchProductModel {
            searchProductModel.hasSelected_PtModel = nil
            searchProductModel.hasSelected_BrModel = nil
            searchProductModel.moq = ""
            searchProductModel.minamount = ""
            searchProductModel.maxamount = ""
            for item in searchProductModel.palist{
                for subItem in item.pavalues{
                    subItem.hasSelected = false
                }
            }
            self.updateViewByModel(searchProductModel)
        }
    }
    ///通过model 更新界面
    func updateViewByModel(_ model:SearchProductModel) -> Void {
        if let brid = self.brid {
            self.pp_Name_Lb.text = ""
            for item in model.brlist{
                if item.brid == brid{
                    self.pp_Name_Lb.text = item.brname
                    self.searchProductModel?.hasSelected_BrModel = item
                    break
                }
            }
        }else{
            if let brmodel = model.hasSelected_BrModel {
                self.pp_Name_Lb.text = brmodel.brname
            }else{
                self.pp_Name_Lb.text = "不限"
            }
        }
        if let ptmodel = model.hasSelected_PtModel {
            self.lm_Name_Lb.text = ptmodel.ptname
        }else{
            self.lm_Name_Lb.text = "不限"
        }
        self.sx_Name_Lb.text = model.showPavstr
        self.moq_Tf.text = model.moq
        self.minamount_Tf.text = model.minamount
        self.maxamount_Tf.text = model.maxamount
    }
}
extension FilterPage_Vc : UITextFieldDelegate{
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField == self.moq_Tf {
            self.searchProductModel?.moq = textField.text ?? ""
        }else if textField == self.minamount_Tf {
            self.searchProductModel?.minamount = textField.text ?? ""
        }else if textField == self.maxamount_Tf {
            self.searchProductModel?.maxamount = textField.text ?? ""
        }
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
        if range.length == 1 {
            return true
        }
        switch string {
        case "0","1","2","3","4","5","6","7","8","9":
            let array = Array(arrayLiteral: textField.text)
            if array.count >= 3 {
                showMsg(100011001)
                return false
            }
            return true
        default:
            showMsg(100011001)
            return false
        }
    }

}
